/** Init **/
window.fbAsyncInit = function () {
    FB.init({
        appId: '235383066660077', //appID di facebook
        status: true,
        xfbml: true
    });
    showFBButtonIfIsLogged();
};

(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));



/** Variabili Globali **/

var FBgraph = {}
var FBapiCaller_count = 0;
var datiutente ="id;name;birthday;gender;hometown;\n";
var datiPostUtenti ="";
var itutenti =0;
var itpost=0;


error = 0;



//Funzione che effettua il login e mostra i pulsanti una volta effettuato
function FB_login() {
    FB.login(function (response) {
        if (response.authResponse) {
            showFBButton();
        }
        else
            console.log('Authorization failed.');
    }, { scope: 'email,user_likes,user_birthday,user_hometown,friends_hometown,friends_birthday,friends_likes,read_stream' });
}

//Preleva i pulsanti dall'html e li rende visibili
function showFBButton() {
    document.getElementById("facebook_login_button").style.display = 'none';
    document.getElementById("facebook_logout_button").style.display = 'block';
    document.getElementById("getFriendPost_button").style.display = 'block';
    document.getElementById("printfile").style.display = 'block';
}

//Funzione che permette il logout
function FB_logout() {
    FB.logout(function () { document.location.reload(); });
}

//Controlla se si è loggati al caricamento ed in caso positivo mostra i pulsanti
function showFBButtonIfIsLogged() {
    FB.getLoginStatus(function (response) {
        if (response.status === 'connected' || response.status === 'not_authorized')
            showFBButton();
    });
}

function getFriendsPosts() {
    var id;
    FB.api('/me',  function(response) {
        id_name = response.id +";"+response.name+"\n";
        datiutente += response.id + ";"+ response.name +";"+  response.birthday + ";" + response.gender +";";
        if(response.hometown === undefined){
            datiutente += "null;\n";
        }
        else{
            datiutente += response.hometown.name + ";\n";
        }
        console.log(datiutente);
        s = document.getElementById("users")
            s.value = datiutente;
    });
    FB.api('/me/statuses?limit=1000', function(response) {
        var message = response.data;
        console.log(response.data);
        var messages = "user="+id_name;
        for(i=0; i<message.length; i++){
            messages += message[i].id + ";"+ message[i].message+";"+ message[i].updated_time+";\n";
        }
        console.log(messages);
        datiPostUtenti += messages;
        s = document.getElementById("posts")
            s.value = datiPostUtenti;
    });

    FB.api('/' + "me" + '/' + "friends", function (response) {
    console.log(JSON.stringify(response.error));
    if (response.data) {
            max = response.data.length;
            console.log(max);
            response.data.forEach(function (friend) {
               getInfo(friend.id, friend.name, max);
            });
            
    }
    });
}



function getInfo(id, name, max) {
    FB.api('/'+id,  function(response) {
        itutenti++;
        datiutente += response.id + ";"+ response.name +";"+ response.birthday + ";" + response.gender +";";
        if(response.hometown === undefined){
            datiutente += "null;\n";
        }
        else{
            datiutente += response.hometown.name + ";\n";
        }
        console.log(itutenti);
        if (itutenti == max) {
            s = document.getElementById("users")
            s.value = datiutente;

            console.log("users fatto");
        }
    });
    
    FB.api(id+"/statuses?limit=1000", function(response) {
        itpost++;
        var message = response.data;
        var messages = "user="+id + ";"+ name +"\n";
        for(i=0; i<message.length; i++){
            msg = message[i].message;
            if(!(msg===undefined)){
            msg = msg.replace(/\;/g,' ');
            messages += message[i].id + ";"+ msg+";"+ message[i].updated_time+";\n";
            }
        }
        datiPostUtenti += messages;
        console.log(itpost);
        if (itpost == max) {
            s = document.getElementById("posts")
            s.value = datiPostUtenti;
            console.log("post fatto");
        }
    });
}

